<style>
    #medicine_man {
        overflow: auto;
        padding: 0;
        margin: 0;
    }
</style>
<div class="easyui-layout" data-options="border:false,fit:true" id="medicine_man">
  <table id="dg" class="easyui-datagrid" style="width:100%;height:auto"
			data-options="
				iconCls: 'icon-edit',
				singleSelect: true,
				url: '<?php echo CController::createUrl('medicine/Getwestern_'); ?>',
				method:'post',
				pagination: true,
        		pageSize: 20,
				onClickCell: onClickCell,
				toolbar: '#tb'
			">
		<thead>
			<tr>
			
			<th data-options="field:'medicine_name',width:80,align:'right',editor:'text'">药品名称</th>
			<th data-options="field:'category',width:80,align:'right',editor:'text'">分类</th>
			<th data-options="field:'unit',width:80,editor:'text'">包装</th>
			<th data-options="field:'price',width:80,editor:'text'">价格</th>
			<th data-options="field:'bar_code',width:110,height:40,editor:'textarea',multiline:'true'">条形码</th>
			<th data-options="field:'specification',width:80,editor:'text'">规格</th>
			<th data-options="field:'inventory',width:80,editor:'text',multiline:'true'">库存</th>
			<th data-options="field:'remark',width:80,editor:'textarea'">标记</th>
		</tr>
		</thead>
	</table>

	<div id="tb" style="height:auto">
	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">保存</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">删除</a>
	
	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">添加</a>
	 <input class="easyui-searchbox product_name_ch" id="medicine_name"  style="width:150px" /><a href="javascript:void(0)" class="easyui-linkbutton" id="search_medicine" data-options="iconCls:'icon-search',plain:true">查找</a>
	 <input class="easyui-searchbox product_name_ch" type="hidden" id="hidden_op" value="add"/>
	</div>
</div>
 
	<script type="text/javascript">
	$('#search_medicine').click(function(){
	        $('#dg').datagrid('load',{
	            medicine_name : $('#medicine_name').val(),
	            url: "<?php echo CController::createUrl('medicine/Getwestern_'); ?>",
	            onClickRow: onClickCell,
	            toolbar: '#tb',
	            iconCls: 'icon-edit',
				singleSelect: true
	        })
	    }) 
		$.extend($.fn.datagrid.methods, {
			editCell: function(jq,param){
				return jq.each(function(){
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field){
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
		});

		jQuery.extend(jQuery.fn.datagrid.methods, {
		editCell: function(jq,param){
			return jq.each(function(){
				var opts = jQuery(this).datagrid('options');
				var fields = jQuery(this).datagrid('getColumnFields',true).concat(jQuery(this).datagrid('getColumnFields'));
				for(var i=0; i<fields.length; i++){
					var col = jQuery(this).datagrid('getColumnOption', fields[i]);
					col.editor1 = col.editor;
					if (fields[i] != param.field){
						col.editor = null;
					}
				}
				jQuery(this).datagrid('beginEdit', param.index);
				for(var i=0; i<fields.length; i++){
					var col = jQuery(this).datagrid('getColumnOption', fields[i]);
					col.editor = col.editor1;
				}
			});
		}
		});
		
		var editIndex = undefined;
		function endEditing(){
			if (editIndex == undefined){return true}
			if ($('#dg').datagrid('validateRow', editIndex)){
				$('#dg').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickCell(index, field){
			if (endEditing()){
				$('#dg').datagrid('selectRow', index)
						.datagrid('editCell', {index:index,field:field});
				editIndex = index;
			}
		}

		function accept(){
			if (endEditing()){

				var rows = $('#dg').datagrid('getChanges');
				if(rows[0].hasOwnProperty('_id')){//update
					rows = JSON.stringify(rows);
					$.ajax({
			            type: 'POST',
			            url: '<?php echo CController::createUrl('medicine/Upwestern_') ;?>',
			            data: {newdata:rows},
			            success: function(data){
			                if(data.status==0){
			                    $('#dg').datagrid('acceptChanges');
			        			$('#dg').datagrid('load');
			                }else{
			                    
			                }
			            },
			            dataType: 'json'
			        });

				}else{//add
					if(rows[0].medicine_name == ''){
						alert('药品名称不能为空');
						return false;
					}
					rows = JSON.stringify(rows);
					$.ajax({
			            type: 'POST',
			            url: '<?php echo CController::createUrl('medicine/addwestern_') ;?>',
			            data: {newdata:rows},
			            success: function(data){
			                if(data.status==0){
			                    $('#dg').datagrid('acceptChanges');
			        			$('#dg').datagrid('load');
			                }else{
			                    
			                }
			            },
			            dataType: 'json'
			        });
			        
				}
				
			}
		}
		function reject(){
			$('#dg').datagrid('rejectChanges');
			editIndex = undefined;
		}
		function removeit(){
	        if (editIndex == undefined){return}
	        $.messager.confirm('删除警告', '你确定要删除吗?', function(r){
				if (r){
					$('#dg').datagrid('cancelEdit', editIndex)
	                .datagrid('deleteRow', editIndex);
	        		editIndex = undefined;
	        		var rows = $('#dg').datagrid('getChanges');
	        		rows = JSON.stringify(rows);
					$.ajax({
			            type: 'POST',
			            url: '<?php echo CController::createUrl('medicine/delwestern_') ;?>',
			            data: {newdata:rows},
			            success: function(data){
			                if(data.status==0){
			                    $('#dg').datagrid('acceptChanges');
			        			$('#dg').datagrid('load');
			                }else{
			                    
			                }
			            },
			            dataType: 'json'
			        });
			       
				}
			});
	        
	    }
	    function append(){
	        if (endEditing()){
	        	$('#hidden_op').val('add');
	            $('#dg').datagrid('appendRow',{isNewRecord:true});
	            editIndex = $('#dg').datagrid('getRows').length-1;
	            $('#dg').datagrid('selectRow', editIndex)
	                    .datagrid('beginEdit', editIndex);
	        }
	    }
	</script>